$(document).ready(function(){
	$('#tz').bind('click',submitTZ);
})

var pycount;
var pkcount;
var ykcecount;
var status;
function getInfo(){
	var checkNo = $.trim($('#CHECK_NO').val());
	if(checkNo == ''){
		alert('请输入盘点单号');
		return;
	}
	cw.common.forLoading('#PY_LIST');
	cw.common.forLoading('#PK_LIST');
	$('#py_count').html('0');
	$('#pk_count').html('0');
	$('#ykce_count').html('0');
	var url = cw.common.path + '/json?action=QUERY_PY_AND_PK_ACTION&CHECK_NO='+checkNo;
	$.post(url,{},function(data){
		if(data.success){
			var pylist = data.root.PY_LIST.rs;
			var pklist = data.root.PK_LIST.rs;
			status = data.root.STATUS.rs[0];
			pycount = data.root.PY_COUNT.rs[0];
			pkcount = data.root.PK_COUNT.rs[0];
			
			var pystr = '';
			$.each(eval(pylist),function(i,obj){
				pystr += "<li id='py"+obj.ID+"'><em style='width: 9%; text-align: center;'><input type='hidden' name='pyid' value='"+obj.ID+"' /><a href='javascript:delPY("+obj.ID+","+obj.STOCK_PRICE+")' title='删除'><img src='../images/icon_01.GIF' /></a></em>";
				pystr += "<em style='width: 30%; text-align: center;''>"+obj.BARCODE+"</em>";
				pystr += "<em style='width: 30%; text-align: center;'>"+obj.FIXED_ASSETS_NAME+"</em>";
				pystr += "<em style='width: 30%; text-align: center;'>"+obj.STOCK_PRICE+"元</em></li>";
				pystr +="</li>";
			})
			$('#PY_LIST').html(pystr);
			
			var pkstr = '';
			$.each(eval(pklist),function(i,obj){
				pkstr += "<li id='pk"+obj.ID+"'><em style='width: 9%; text-align: center;'><input type='hidden' name='pkid' value='"+obj.ID+"' /></em>";//<a href='javascript:delPK("+obj.ID+","+obj.STOCK_PRICE+")' title='删除'><img src='../images/icon_01.GIF' /></a>
				pkstr += "<em style='width: 30%; text-align: center;''>"+obj.BARCODE+"</em>";
				pkstr += "<em style='width: 30%; text-align: center;'>"+obj.FIXED_ASSETS_NAME+"</em>";
				pkstr += "<em style='width: 30%; text-align: center;'>"+obj.STOCK_PRICE+"元</em></li>";
				pkstr +="</li>";
			})
			$('#PK_LIST').html(pkstr);
			
			if(pylist.length == 0){
				$('#PY_LIST').html('<li><em style="width: 99%; text-align: center;">无盘盈数据......</em></li>');
			}
			
			if(pklist.length == 0){
				$('#PK_LIST').html('<li><em style="width: 99%; text-align: center;">无盘亏数据......</em></li>');
			}
			
			$('#py_count').html(pycount);
			$('#pk_count').html(pkcount);
			ykcecount = pkcount-pycount;
			$('#ykce_count').html(ykcecount);
		}
		else{
			alert("查询失败");
		}
	},'json');
}

function delPY(id,price){
	if(confirm("是否删除当前盘盈信息？")){
		pycount = pycount-price;
		$('#py_count').html(pycount);
		ykcecount = pkcount-pycount;
		$('#ykce_count').html(ykcecount);
		$('#py'+id).remove();
	}
	
}

function delPK(id,price){
	if(confirm("是否删除当前盘亏信息？")){
		pkcount = pkcount-price;
		$('#pk_count').html(pkcount);
		ykcecount = pkcount-pycount;
		$('#ykce_count').html(ykcecount);
		$('#pk'+id).remove();
	}
	
}

function submitTZ(){
	var checkNo = $.trim($('#CHECK_NO').val());
	if(checkNo == ''){
		alert('请先查询盘点单！');
		return;
	}
	if(pkcount == 0 && pycount == 0){
		alert("没有可提交的调账信息！");
		return;
	}
	if(status == '2'){
		alert("当前盘点单已完结，不能再进行调账！");
		return;
	}
	if(confirm("是否确定按此盈亏结果调整资产信息？")){
		var pyobj = document.getElementsByName('pyid');
		var pyids = [];
		for(var i = 0;  i < pyobj.length; i++){
			pyids.push(pyobj[i].value);
		}
		
		var pkobj = document.getElementsByName('pkid');
		var pkids = [];
		for(var i = 0;  i < pkobj.length; i++){
			pkids.push(pkobj[i].value);
		}
		
		$.post(cw.common.path + '/json?action=ADJUST_ACTION',{
			CHECK_NO : checkNo,
			CHENG_BEFULLOF : pycount,
			CHENG_LOSE : pkcount,
			BEFULLOF_LOSE : ykcecount,
			PY_IDS : pyids,
			PK_IDS : pkids
		},function(data){
			if(data.success){
				alert("调账成功！");
				getInfo();
			}
			else{
				alert("调账失败！");
			}
		},'json');
	}
}

